package com.hushan.elevator.service.impl;

import com.hushan.elevator.dao.DeviceInfoRepository;
import com.hushan.elevator.dao.MntPersonRepository;
import com.hushan.elevator.model.Department;
import com.hushan.elevator.model.DeviceInfo;
import com.hushan.elevator.model.MntPerson;
import com.hushan.elevator.model.User;
import com.hushan.elevator.security.SecurityUtils;
import com.hushan.elevator.service.DeptService;
import com.hushan.elevator.service.DeviceInfoService;
import com.hushan.elevator.service.MntPersonService;
import com.hushan.elevator.utils.Result;
import com.hushan.elevator.utils.ResultCode;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MntPersonServiceImpl implements MntPersonService {
    private final MntPersonRepository mntPersonDao;
    private final DeptService deptService;
    private final DeviceInfoRepository deviceInfoDao;
    public MntPersonServiceImpl(MntPersonRepository mntPersonDao, DeptService deptService, DeviceInfoRepository deviceInfoDao) {
        this.mntPersonDao = mntPersonDao;
        this.deptService = deptService;
        this.deviceInfoDao = deviceInfoDao;
    }


    @Override
    public void addPerson(MntPerson person) {
        person.setUpdatedTime(LocalDateTime.now());
        person.setCreatedTime(LocalDateTime.now());
        person.setIsDeleted(false);
        mntPersonDao.save(person);
    }

    @Override
    public MntPerson getPersonById(int id) {
        return mntPersonDao.findById(id).orElse(null);
    }

    @Override
    public void updatePerson(MntPerson mntPerson) {
        mntPerson.setUpdatedTime(LocalDateTime.now());
        mntPerson.setIsDeleted(false);
        mntPersonDao.save(mntPerson);
    }

    @Override
    public List<MntPerson> getPersonsByDeptId(int deptId) {
        MntPerson mntPerson = new MntPerson();
        mntPerson.setDeptId(deptId);
        mntPerson.setIsDeleted(false);
        Example<MntPerson> example = Example.of(mntPerson);
        return mntPersonDao.findAll(example);
    }

    @Override
    public int findCountByDept(Integer id) {
        MntPerson mntPerson = new MntPerson();
        mntPerson.setDeptId(id);
        mntPerson.setIsDeleted(false);
        Example<MntPerson> example = Example.of(mntPerson);
        return (int) mntPersonDao.count(example);
    }

    @Override
    public boolean detectAuthorize(Integer mntPersonId) {
        User requestUser = SecurityUtils.getUser();
        
        Department personDept = deptService.findDeptById(getPersonById(mntPersonId).getDeptId());

        if (personDept == null) {
            return false;
        }
        // detect dept permission
        return deptService.findUserPermissionForDept(requestUser, personDept);
    }


    @Override
    public void deleteById(int id) throws Exception {
        int device_in_mnt_person = deviceInfoDao.findCountByMntPerson(id);
        if (device_in_mnt_person<0){
            throw new Exception();
        }
        mntPersonDao.deleteById(id);
    }

    @Override
    public MntPerson findByDevice(Integer deviceId) {
        return mntPersonDao.findByDevice(deviceId);
    }
}
